//
//  FileReadStage.m
//  FileManageDemo
//
//  Created by shk on 14/12/3.
//  Copyright (c) 2014年 com.shk. All rights reserved.
//

#import "FileReadStage.h"

@implementation FileReadStage



- (void) readFileStage{
    NSString *homePath = NSHomeDirectory();
    NSString *sourcePath = [homePath stringByAppendingPathComponent:@"temp/oc-file-read-space.jpg"];
    NSString *targetPth = [homePath stringByAppendingPathComponent:@"temp/oc-file-read-space-copy.jpg"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL success = [fileManager createFileAtPath:targetPth contents:nil attributes:nil];//创建文件
    if(success){
        NSLog(@"create success");
    }
    
    NSFileHandle *inFile = [NSFileHandle fileHandleForReadingAtPath:sourcePath];
    NSFileHandle *outFile = [NSFileHandle fileHandleForWritingAtPath:targetPth];
    NSDictionary *fileAttri = [fileManager attributesOfItemAtPath:sourcePath error:nil];//获取源文件的属性
    NSNumber *fileSizeNum = [fileAttri objectForKey:NSFileSize];//取得源文件的大小
    
    BOOL isEnd = YES;
    NSInteger readSize = 0;
    NSInteger fileSize = [fileSizeNum longValue];
    while(isEnd){
        NSInteger subLeng = fileSize - readSize;
        NSData *data = nil;
        if(subLeng < 500){
            data = [inFile readDataToEndOfFile];
            isEnd = NO;
        }else{
            data = [inFile readDataOfLength:500];
            readSize += 500;
            [inFile seekToFileOffset:readSize];//读取之后指针跳转到当前所读的最后位置，以便于xian
        }
        [outFile writeData:data];
    }
    [outFile closeFile];
}

@end
